/**
 * 流程选择通用组件功能
 * 
 * @Date: 2018-10-14
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery", "bsMsgServ","ajaxServ","datagridServ","init","btnServ","bizProto","valueUtil"], 
	function($,bsMsgServ,ajaxServ,datagridServ,init,btnServ,bizProto,valueUtil) {
	
	
	var params ;
	
	var rootid = 'div-flow-select';
	
	var window_id='win-flow-select',	window_height='400',window_width='600';
	
	var pageUrl = '/flowBizAll/query/popPage.action',
	
	listUrl='/flowBizAll/query/getListDomain.action';
	
	var private_method = {
		
			
		//脚本弹出流程选择页面
		openPage:function(_options){
			//缓存传入参数
			params =  _options;
			$('#'+window_id).empty();
			var rootpath = '/'+init.getSite()
			var _config ={
				title	: '选择审批流程',
				width:window_width,
				height:window_height,
				window:$('#'+window_id),
				href:rootpath+pageUrl,
				flag:false,				
				onClose:private_method.onClose
			}
		
			
					
			var _param = $.extend({},_config,_options)
			openParam = _param
			datagridServ.windowEdit(_param);
			
		},
		
		initList:function(_options){
			var _detailParam = {				
				url : '/'+init.getSite() + listUrl,
				onDblClickRow: btn_event.dblClick,
				height:init.getGridHeight(),
				rownumbers:true,
				animate: true,									
				striped:true,
				border:true,
				singleSelect:true,
				idField: 'id',									
				fitColumns: true,
				checkOnSelect:true,
				pagination : true,
				selectOnCheck:true,
				columns:[[
					{field:'ck',checkbox:true},					     
					{field:'name',title:'事项名称',width:100,halign:'center',
						formatter:function(value,row,index){
							if(value){
								return '<span class=/"easyui-tooltip/" title="'+value +'">'+value +'</span>';
							}
							return '';						
						}
					},					
					{field:'apply_deptname',title:'申请部门',width:50,halign:'center'} ,
					{field:'apply_name',title:'申请人',width:50,halign:'center'},
					{field:'apply_date',title:'申请时间',width:50,halign:'center'} ,					
					{field:'status',title:'当前状态',width:50,align:'center'}
					
				]]
			}
			
			if(params.bizcode){
				_detailParam.queryParams={condition:{bizcode:params.bizcode}}
			}
			var _d = $('#'+rootid + ' .dg-list')
			if(_d.length===1){			
				_d.datagrid(_detailParam);
			}
			
			private_method.bind_btn_event();
		}
		
		,
		bind_btn_event:function(){
			var _btn = $('#'+rootid + ' .btn')
			if(_btn.length>0){
				_btn.unbind('click');
				_btn.bind('click',private_method.btn_listenner);
			}
			
		},
		btn_listenner:function(e){
			init.applyBtnEvt({btnEvt:e,btnConfig:btn_event});
		}
	}
	
	var btn_event = {
		
		evtSelect:function(e){
			e.stopPropagation();
			e.preventDefault();
			var _grid = $('#' + window_id + ' .dg-list')
			if(_grid.length===1){
				var _row = _grid.datagrid('getSelections');
				if(_row && _row.length===1){
					if(params && params.callback){
						params.callback(_row[0],params.bizcode);
					}
					$(e.currentTarget).removeClass('disabled');
					$('#' + window_id ).window('close');
					
				}else{
					bsMsgServ.warnSlide('请选择关联的审批记录！');			
				}
			}else{
				bsMsgServ.warnSlide('请选择关联的审批记录！');			
			}
			$(e.currentTarget).removeClass('disabled');
		},
		evtReset:function(e){
			e.stopPropagation();
			e.preventDefault();
			var _grid = $('#' + rootid + ' .dg-list')
			if(_grid.length===1){
				_grid.datagrid('clearSelections');
			}
			$(e.currentTarget).removeClass('disabled');
		},
		dblClick:function(index,row){
			
			var _grid = $('#'+rootid + ' .dg-list')
			_grid.datagrid('clearSelections')
			_grid.datagrid('selectRow',index);
			
			var _btn = $('#' + rootid + " [btn-type='evtSelect']");
			
			if(_btn && _btn.length===1){
				_btn.trigger('click');
			}
//			btnEvent.editExecute({row:row});
		}
	
	}
	
	var public_method = {
			
		openPage:function(_options){
			private_method.openPage(_options);
		}
		,
		
		initList:function(_options){
			private_method.initList(_options);
		}
			
	}
	
	
	return public_method;
	
})